package com.kofan.shelter.commons.util.handler;

import com.kofan.shelter.api.bean.JsonResult;

import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;
import java.util.stream.Collectors;

@RestControllerAdvice
public class BindExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult bindExceptionHandler(MethodArgumentNotValidException e) {
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        List collect = fieldErrors.stream().map(o -> o.getDefaultMessage()).collect(Collectors.toList());
        return JsonResult.error(500,"参数异常",collect);
    }
}
